package com._CPA_.service.impl;

import com._CPA_.dao.RootMapper;
import com._CPA_.pojo.PageBean;
import com._CPA_.pojo.UserInfo;
import com._CPA_.service.RootService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;

import java.time.LocalDate;
import java.util.List;

@Service
public class RootServiceImpl implements RootService {
    @Autowired
    private  RootMapper  rootMapper;

    /**
     * 分页查询逻辑
     * @param page
     * @param pageSize
     * @param username
     * @param gender
     * @param vip
     * @param begin
     * @param end
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize,String username, String gender,Short vip, Short ifAdmin,LocalDate begin, LocalDate end) {
        //1. 设置分页参数
        PageHelper.startPage(page,pageSize);

        //2. 执行查询
        List<UserInfo> userList =  rootMapper.listSelect(username, gender,vip, ifAdmin,begin, end);

        Page<UserInfo> p = (Page<UserInfo>) userList;

        //3. 封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;



    }

    /**
     * 批量删除
     * @param userIds
     */
    @Override
    public void delete(List<String> userIds) {

        rootMapper.myDelete(userIds);
    }



    /**
     * 生成用户唯一的id
     * @return
     */
    private  String creatRootCodeU() {
        UserInfo user=new UserInfo();

        boolean t = true;
        String usercode = "";
        while (t) {
            usercode = String.valueOf((int) ((Math.random() * 9 + 1) * 100000*100));
            user.setUserId(usercode);
            int have = rootMapper.selectCount(user);
            if (have == 0) {
                t = false;
            }
        }
        return usercode;
    }

    // 管理员添加用户
    @Override
    public void RootAdd(UserInfo user) {
        /**
         * 初始化用户信息
         */
        user.setUserId(creatRootCodeU());
        if(user.getGender()==null){
            user.setGender("男");
        }
        if(user.getNickName()==null){
            user.setNickName(user.getUserName());
        }
        if (user.getUserIcon()==null){
            user.setUserIcon("http://43.139.94.130:6900/cpa-images/3A8B3D23203D41A68882E4E614A1DD48.png");
        }
        user.setCreateTime(LocalDateTime.now());
        user.setEditTime(LocalDateTime.now());
        if(user.getIfAdmin()==null)
        user.setIfAdmin((short)0);
        if(user.getIfVip()==null)
        user.setIfVip((short)0);

        user.setUserId(creatRootCodeU());
        rootMapper.insert(user);
    }

    // add用户名唯一
    @Override
    public boolean addIfOnlyName(String userName) {
        if (userName.equals(rootMapper.selectUserName(userName))){
            return false;
        }
        return true;
    }

    // add手机号唯一
    @Override
    public boolean addIfOnlyPhone(String userPhone) {
        if (userPhone.equals(rootMapper.selectPhone(userPhone))){
            return false;
        }
        return true;
    }

    /**
     * edit 手机号唯一
     * @param
     * @return
     */
    @Override
    public boolean editIfOnlyPhone(UserInfo user) {
        Example em =new Example(UserInfo.class);
        Example.Criteria criteria= em.createCriteria();;
        criteria.andEqualTo("phone",user.getPhone());
         List <UserInfo> users=rootMapper.selectByExample(em);
         if (users.size()>1){
             return  false;
         }
         return  true;



    }

    /**
     * edit 用户名唯一
     * @param
     * @return
     */
    @Override
    public boolean editIfOnlyName(UserInfo user) {
        Example em =new Example(UserInfo.class);
        Example.Criteria criteria= em.createCriteria();;
        criteria.andEqualTo("phone",user.getUserName());
        List <UserInfo> users=rootMapper.selectByExample(em);
        if (users.size()>1){
            return  false;
        }
        return  true;

    }


    //  管理员修改用户信息的方法
    @Override
    public void RootUpdate(UserInfo user) {
        user.setEditTime(LocalDateTime.now());
        rootMapper.isUpdate(user);
    }

    /**
     * 密码登录
     * @param user
     * @return
     */
    @Override
    public UserInfo rootLoginByName(UserInfo user) {
        Example em = new Example(UserInfo.class);//可以封装任何条件,需要给一个泛型
//       将条件封装到example的内部类
        Example.Criteria criteria = em.createCriteria();
//       查询条件
        criteria.andEqualTo("userName",user.getUserName());
        criteria.andEqualTo("password",user.getPassword());
        return rootMapper.selectOneByExample(em);//根据条件返回对应的用户对象的所有信息
    }

    /**
     * 手机号登录
     * @param user
     * @return
     */
    @Override
    public UserInfo rootLoginByPhone(UserInfo user) {
        Example em =new Example(UserInfo.class);
        Example.Criteria criteria= em.createCriteria();;
        criteria.andEqualTo("phone",user.getPhone());
        criteria.andEqualTo("password",user.getPassword());
        return  rootMapper.selectOneByExample(em);
    }

    @Override
    public UserInfo login(UserInfo user) {
        return rootMapper.login(user);
    }
}
